import * as actionTypes from './constants';

import { 
  getTopBanners, 
  getHotRecommends, 
  getNewAlbums, 
  getRanking,
  getArtistList 
} from "@/services/recommend";


/** 轮播图 */
//2.放到reducer
const changeTopBannersAction = (res) => ({
  type: actionTypes.CHANGE_TOP_BANNERS,
  topBanners: res.banners
})
//1.通过thunk发起网络请求数据
export const getTopBannersAction = () => {
  return dispatch => {
    getTopBanners().then((res) => {
      dispatch(changeTopBannersAction(res))
    })
  }
}

 
/** 热门推荐 */
export const getHotRecommendsAction = () => {
  return dispatch => {
    getHotRecommends().then(res => {
      dispatch(changeHotRecommendsAction(res))
    })
  }
}
const changeHotRecommendsAction = (res) => ({
  type: actionTypes.CHANGE_HOT_RECOMMENDS,
  hotRecommends: res.result
})

/** 新碟上架 */
export const getNewAlbumsAction = (limit) => {
  return dispatch => {
    getNewAlbums(limit, 0).then(res => {
      dispatch(changeNewAlbumsAction(res))
    })
  }
}
const changeNewAlbumsAction = (res) => ({
  type: actionTypes.CHNAGE_NEW_ALBUMS,
  newAlbums: res.albums
})

/** 榜单 
 * 3779629 === 新歌榜
 * 2884035 === 原创榜
 * 19723756 === 飙升榜
*/
export const getRankingAction = (idx) => {
  return dispatch => {
    getRanking(idx).then(res => {
      switch (idx) {
        case 3779629:
          dispatch(changeNewRanking(res));
          break;
        case 2884035:
          dispatch(changeOriginRanking(res));
          break;
        case 19723756:
          dispatch(changeUpRanking(res));
          break;
        default:
      }
    })
  }
}
const changeUpRanking = (res) => ({
  type: actionTypes.CHNAGE_UP_RANKING,
  upRanking: res.playlist
});
const changeNewRanking = (res) => ({
  type: actionTypes.CHNAGE_NEW_RANKING,
  newRanking: res.playlist
});
const changeOriginRanking = (res) => ({
  type: actionTypes.CHNAGE_ORIGIN_RANKING,
  originRanking: res.playlist
})

/**入驻歌手 */
export const getSettleSingers = (limit, cat) => {
  return dispatch => {
    getArtistList(limit, cat).then(res => {
      dispatch(getSettleSingersAction(res.artists))
    })
  }
}
const getSettleSingersAction = (settleSings) => ({
  type: actionTypes.CHANGE_SETTLE_SINGER,
  settleSings
})